package com.yehaikeji.workflow.controller.management;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yehaikeji.workflow.controller.bean.ResultMessage;

import io.swagger.annotations.ApiOperation;

@RequestMapping("fileuploads")
@Controller
public class fileuploadsController {

	// 项目下路径位置
	String uploademun = "/resource/upload";

	@RequestMapping(value = "morefileupload", method = RequestMethod.POST)
	@ApiOperation(value = "上传文件公共接口")
	@ResponseBody
	public ResultMessage fileuploads(@RequestParam(value = "file") MultipartFile file[], HttpServletRequest request) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			
			if (file.length == 0) {
				resultMessage.setMessage("没有文件");
				resultMessage.setSuccess(false);
			} else {
				List<String> list=new ArrayList<>();
				for (MultipartFile multipartFile : file) {
					if (multipartFile.isEmpty()) {
						System.out.println("文件为空");
					} else {
						// 限制大小
						if (multipartFile.getSize() > 1024 * 1024 * 3) {

						} else {
							// 创建网络资源路径
							String realurl = request.getScheme() + "://" + request.getServerName() + ":"
									+ request.getServerPort() + request.getContextPath()+uploademun;
							// 创建真实保存路径
							String realpath = request.getSession().getServletContext().getRealPath("")+uploademun;
							System.out.println("linux-path:"+realpath);
							String name=multipartFile.getOriginalFilename();
							String newname= UUID.randomUUID().toString().replaceAll("-", "")+name.substring(name.lastIndexOf("."));
							File file2=new File(realpath+"/"+newname);
							multipartFile.transferTo(file2);
							list.add(realurl+"/"+newname);
						}
					}
				}
				resultMessage.setData(list);
				resultMessage.setSuccess(true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}

		return resultMessage;
	}
}
